package orm.jpa.first.model;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Transient;



@Entity
@Access(AccessType.FIELD)
public class Employee {
     
    public static final String LOCAL_AREA_CODE = "613";
     
    @Id
    private int id;
    private String name;
    private long salary;
    @Transient
    private String phoneNum;
     
    public Employee() {
    	System.out.println("디폴트 생성자 : " + phoneNum);
    }
    public Employee(int id, String name, long salary) {
        this.id = id;
        this.name = name;
        this.salary = salary;
        
        System.out.println("타락한 생성자 : " + phoneNum);
    }
     
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public long getSalary() {
        return salary;
    }
    public void setSalary(long salary) {
        this.salary = salary;
    }
    public String getPhoneNum() {
        return phoneNum;
    }
    public void setPhoneNum(String phoneNum) {
        this.phoneNum = phoneNum;
    }
     
    @Access(AccessType.PROPERTY) 
    @Column(name="PHONE")
    protected String getPhoneNumberForDb() {
    	
    	Throwable t = new Throwable();
    	t.printStackTrace();
    	
    	System.out.println("get : " + phoneNum);
    	
        if (phoneNum.length() == 10)
            return phoneNum;
        else
            return LOCAL_AREA_CODE + phoneNum;
    }
    protected void setPhoneNumberForDb(String num) {
    	System.out.println("set : " + phoneNum);

    	if (num.startsWith(LOCAL_AREA_CODE))
            phoneNum = num.substring(3);
        else
            phoneNum = num;
    }
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("Employee [id=").append(id).append(", name=")
				.append(name).append(", salary=").append(salary)
				.append(", phoneNum=").append(phoneNum).append("]");
		return builder.toString();
	}
}
